﻿using System;
using System.Windows.Forms;
using xClient.Framework;
using xClient.Framework.BaseView;
using xClient.Framework.PlugCore;

namespace cMES1008
{
    //关键料头维护程序
    [Plug(ID = "cMES1008",
            Name = "关键料头基础信息",
            Description = "关键料头基础资料维护",
            Parent = "0,基础资料")]
    public partial class cMES1008 : StdView
    {
        public cMES1008()
        {
            InitializeComponent();
        }
        
        //get prefix list
        private void GetKPNPrefixList(string vKPN=null,string vCustCode=null)
        {
            ExecutionResult<dynamic> _res;
            _res = Services.ServiceMethod.GetKPNPrefixList(vKPN,vCustCode);
            if (!_res.Status)
            {
                ShowMessage(_res.Message, MsgType.NG);
                return;
            }
            datalist.DataSource = _res.Data;
            datalist.ClearSelection();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            var _kpnPrefix = this.txtSearch.Text.Trim().ToUpper();
            var _custCode = this.txtCustCode.Text.Trim().ToUpper();

            var _res = Services.ServiceMethod.GetKPNPrefixList(_kpnPrefix, _custCode);
            if (!_res.Status)
            {
                ShowMessage(_res.Message, MsgType.NG);
                return;
            }
            if (_res.Data.Count > 0)
            {
                datalist.DataSource = _res.Data;
            }
            else
            {
                datalist.Rows.Clear();
            }
            datalist.ClearSelection();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.KPNPrefixChange(sender);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            this.KPNPrefixChange(sender);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (datalist.SelectedRows.Count < 1)
            {
                this.ShowMessage("请选择一笔需要删除的资料..", MsgType.NG);
                return;
            }
            var _prefix_id = datalist.SelectedRows[0].Cells[0].Value;
            var _prefix_KPN = datalist.SelectedRows[0].Cells[1].Value;

            DialogResult dialog = MessageBox.Show(
                "确定要删除 " + _prefix_KPN + "吗?", "系统提示",
                MessageBoxButtons.YesNo,
                 MessageBoxIcon.Warning,
                  MessageBoxDefaultButton.Button2);
            if (dialog == DialogResult.Yes)
            {
                var result = Services.ServiceMethod.DeleteKPNPrefix(_prefix_id.ToString());
                if (result.Status)
                {
                    this.ShowMessage("删除数据成功..", MsgType.OK);
                    //刷新数据
                    this.GetKPNPrefixList(null, null);
                }
            }
        }

        private void KPNPrefixChange(object sender)
        {
            frmKPNPrefix _kpnEdit = default(frmKPNPrefix);
            var clickBtnTag = ((ToolStripButton)sender).Tag;
            if (clickBtnTag.Equals("ADD"))
            {
                _kpnEdit = new frmKPNPrefix(clickBtnTag.ToString());
                DialogResult _result = _kpnEdit.ShowDialog();
                if(_result == DialogResult.OK)
                {
                    this.ShowMessage("数据新增成功..", MsgType.OK);
                    //refresh data
                    this.GetKPNPrefixList();
                }
                else if(_result == DialogResult.No)
                {
                    this.ShowMessage("数据新增失败..", MsgType.NG);
                    //refresh data
                    this.GetKPNPrefixList();
                }
            }
            else if(clickBtnTag.Equals("EDIT"))
            {
                if (datalist.SelectedRows.Count < 1)
                {
                    this.ShowMessage("请选择一笔需要修改的资料.", MsgType.NG);
                    return;
                }
                _kpnEdit = new frmKPNPrefix(clickBtnTag.ToString(), datalist.SelectedRows[0]);
                DialogResult _result = _kpnEdit.ShowDialog();
                if (_result == DialogResult.OK)
                {
                    this.ShowMessage("数据修改成功..", MsgType.OK);
                    //refresh data
                    this.GetKPNPrefixList();
                }
                if (_result == DialogResult.No)
                {
                    this.ShowMessage("数据修改失败..", MsgType.NG);
                    //refresh data
                    this.GetKPNPrefixList();
                }
            }
        }

        private void cMES1008_Load(object sender, EventArgs e)
        {
            this.GetKPNPrefixList();
        }

        private void datalist_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                frmKPNPrefix _kpnForm = new frmKPNPrefix("EDIT", datalist.Rows[e.RowIndex]);

                DialogResult dialog = _kpnForm.ShowDialog();
                if (dialog == DialogResult.OK)
                {
                    this.ShowMessage("修改成功.", MsgType.OK);
                }
                else if (dialog == DialogResult.No)
                {
                    this.ShowMessage("修改成功.", MsgType.OK);
                }
                this.GetKPNPrefixList();
            }
        }
    }
}
